package testdemo;

import dao.FindEmpData;
import dao.UserMapperDao;
import javabean.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;
import tools.ThingManage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

/**
 * @author 路老师
 * @date 2025/3/25
 * 无精神，不编程！无乐趣，不编程！
 * “调试：与bug共舞的艺术。”
 */
public class TestMybatis1 {
    public static void main(String[] args) throws IOException {


    }

    @Test
    /**
     * 查询数据
     */
    public void findData() throws IOException {
        //输出查询过程日志信息
        ThingManage.startThing();

        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        //4.执行映射文件中的sql语句
        List<Object> objects = sqlSession.selectList("EmpMapper.findAllData");
//        System.out.println("objects = " + objects);
        for (Object obj : objects) {
            System.out.println(obj);
        }
    }

    /**
     * 插入数据
     *
     * @throws IOException
     */

    public Integer insertData() throws IOException {
        //输出查询过程日志信息

        ThingManage.startThing();

        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        int update = sqlSession.update("EmpMapper.insertdata");
        //提交事务
        sqlSession.commit();
        System.out.println("update = " + update);
        return update;

    }

    /**
     * 更新数据
     *
     * @throws IOException
     */
    @Test
    public void updateData() throws IOException {
        //输出查询过程日志信息
        ThingManage.startThing();

        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        int update = sqlSession.update("EmpMapper.updata");
        //提交事务
        sqlSession.commit();
        System.out.println("update = " + update);


    }

    /**
     * 删除数据
     *
     * @throws IOException
     */
    @Test
    public void delData() throws IOException {
        //输出查询过程日志信息
        ThingManage.startThing();

        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        int update = sqlSession.update("EmpMapper.deletedata");
        //提交事务
        sqlSession.commit();
        System.out.println("update = " + update);

    }

    /**
     * 查询一条数据
     *
     * @throws IOException
     */
    @Test
    public void findOneData() throws IOException {
        //输出查询过程日志信息
        ThingManage.startThing();

        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();
        Object o = sqlSession.selectOne("EmpMapper.findOne", 51);
        System.out.println("o = " + o);
    }


    /**
     * 插入一条数据
     *
     * @throws IOException
     */
    @Test
    public void insertOneData() throws IOException {
        //输出查询过程日志信息
        ThingManage.startThing();
        //2.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        //准备数据
        HashMap objectObjectHashMap = new HashMap();
        objectObjectHashMap.put("name", "lisi666");
        objectObjectHashMap.put("job", "C++攻城狮");
        objectObjectHashMap.put("salary", 12000);

        int update = sqlSession.update("EmpMapper.insertdata1", objectObjectHashMap);
        sqlSession.commit();
        System.out.println("update = " + update);


    }

    /**
     * 更新一条数据
     *
     * @throws IOException
     */
    @Test
    public void updateOneData() throws IOException {

        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        Emp emp = new Emp();
        emp.setId(71);
        emp.setName("lisi890");
        emp.setJob("java架构师9999");
        emp.setSalary(13000.0);


        int update = sqlSession.update("EmpMapper.update1", emp);
        sqlSession.commit();
        System.out.println("update = " + update);


    }


    /**
     * 删除一条数据
     *
     * @throws IOException
     */
    @Test
    public void delOneData() throws IOException {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();
        int delete = sqlSession.delete("EmpMapper.delete2", 77);
        sqlSession.commit();
        System.out.println("delete = " + delete);


    }


@Test
    public void testFindData()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        HashMap hashMap = new HashMap();
        hashMap.put("cols","name,salary");
        List<Object> objects = sqlSession.selectList("EmpMapper.findData3", hashMap);
        for (Object obj : objects) {
            System.out.println(obj);
        }

    }


    /**
     * 1、模糊查询
     */
    @Test
    public void findByName()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        HashMap hashMap = new HashMap();
        hashMap.put("name","%云%");

        List<Object> objects = sqlSession.selectList("EmpMapper.findName", hashMap);
        for (Object obj : objects) {
            System.out.println(obj);
        }
    }

    /**
     * 2、找出某个区间的所有记录
     */
    @Test
    public void findSalaryTest()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("minSal",6000);
        stringObjectHashMap.put("maxSal",10000);

        List<Object> objects = sqlSession.selectList("EmpMapper.findSalary", stringObjectHashMap);
        for (Object obj : objects) {
            System.out.println(obj);
        }
    }


    /**
     * 3、同时删除多条数据记录
     */
    @Test
    public void deleteByIdsTest()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();
        Integer [] ids={8,19,21};
        int delete = sqlSession.delete("EmpMapper.deleteByIds", ids);
        sqlSession.commit();
        System.out.println("delete = " + delete);
    }




    /**
     * 测试接口接口代理的方式
     */

    @Test
    public void findByIdsTest()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        FindEmpData mapper = sqlSession.getMapper(FindEmpData.class);
        Emp byIdTest = mapper.findByIdTest(15);
        System.out.println("byIdTest = " + byIdTest);
    }


    /**
     *
     */
    @Test
    public void findAllDataTest()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        FindEmpData mapper = sqlSession.getMapper(FindEmpData.class);

        List<Emp> allData = mapper.findAllData();
        for(Emp e:allData)
        {
            System.out.println("e = " + e);
        }
    }


    /**
     *
     */
    @Test
    public void insertUserTest()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        UserMapperDao mapper = sqlSession.getMapper(UserMapperDao.class);

        int i = mapper.insertUser();

        sqlSession.commit();
        System.out.println("i = " + i);

    }

    @Test
    public void deleteUserTest()
    {
        //输出查询过程日志信息
        ThingManage.startThing();
        //3.通过工厂获取对象
        SqlSession sqlSession = ThingManage.getSqlSession();

        UserMapperDao mapper = sqlSession.getMapper(UserMapperDao.class);

         mapper.deleteUser();

        sqlSession.commit();

    }

}
